/**
 * 项目名：student
 * 修改历史：
 * 作者： MZ
 * 创建时间： 2016年1月6日-上午9:43:48
 */
package com.cxy.web.swing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.springframework.context.ApplicationContext;

import com.cxy.common.Const;
import com.cxy.entity.SysRole;
import com.cxy.entity.SysUser;
import com.cxy.service.UserService;
import com.cxy.util.SpringContextUtils;

/**
 * 模块说明： 登录界面
 * 
 */
public class LoginView extends JFrame {

	ApplicationContext ctx = SpringContextUtils.getApplicationContext();	// 获取Spring上下文对象	
	private UserService userService = (UserService) ctx.getBean("userService");
	
	private static final long serialVersionUID = -5278598737087831336L;
	private JPanel jPanelCenter, jPanelSouth;
	private JTextField username;
	private JPasswordField password;
	private JButton loginButton, resetButton;
	


	public LoginView() {
		init();
	}

	private void init() {
		this.setTitle("Login");

		jPanelCenter = new JPanel();
		jPanelCenter.setLayout(new GridLayout(3, 2));
		jPanelCenter.add(new JLabel(Const.LOGIN_USERNAME));
		username = new JTextField();
		jPanelCenter.add(username);
		jPanelCenter.add(new JLabel(Const.LOGIN_PASSWORD));
		password = new JPasswordField();
		// enter key listener
		password.addKeyListener(new LoginListener());
		jPanelCenter.add(password);
		//jPanelCenter.add(new JLabel("----------------------------------------------"));
		//jPanelCenter.add(new JLabel("----------------------------------------------"));

		jPanelSouth = new JPanel();
		jPanelSouth.setLayout(new GridLayout(1, 2));
		loginButton = new JButton(Const.LOGIN);
		loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				check();
			}
		});
		jPanelSouth.add(loginButton);
		resetButton = new JButton(Const.RESET);
		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText("");
				password.setText("");
			}
		});
		jPanelSouth.add(resetButton);

		this.add(jPanelCenter, BorderLayout.CENTER);
		this.add(jPanelSouth, BorderLayout.SOUTH);

		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setBounds(450, 250, 375, 140);
		this.setResizable(false);
		this.setVisible(true);
	}

	private class LoginListener extends KeyAdapter {

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				check();
			}
		}
	}

	/**
	 * 检查用户名密码
	 */
	private void check() {		
		SysUser user = userService.findByUserNameAndUserPassword(username.getText(), String.valueOf(password.getPassword()));
		
		String roleName = "";
		Iterator<SysRole> it = user.getUser_role().iterator();
		while(it.hasNext()){		
			roleName = ((SysRole)it.next()).getRoleName();
		}
		System.out.println("-------------------------roleName:"+roleName);
		
		/**
		 * 判断是管理员登陆还是普通用户登陆
		 * 管理员:跳转到管理页面
		 * 普通用户:跳到抽奖页面
		 */
		if (user!=null && "管理员".equals(roleName)){
			dispose();	// 关闭登陆页面
			new MainView();
		}else if(user!=null && "普通用户".equals(roleName)){
			dispose();	// 关闭登陆页面
//	        GodLucky rn = new GodLucky(user);
//	        Thread t1 = new Thread(rn);
//	        t1.start();
			new LuckDrawView(user);
		}else {
			username.setText("");
			password.setText("");
		}
	}

}
